package com.ylr.config.framework.service.impl;

import com.ylr.config.common.enums.MappingRecordEnum;
import com.ylr.config.framework.entity.MappingRecordEntity;
import com.ylr.config.framework.mapper.MappingRecordMapper;
import com.ylr.config.framework.service.MappingRecordService;
import com.ylr.base.common.enums.CommonEnum;
import com.ylr.base.common.po.user.UserPo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ylr.base.common.utils.UserUtils;
import com.ylr.base.framework.service.SnowflakeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-02 19:14:43
 * className: MappingRecordServiceImpl
 * version: 1.0
 * description:
 */
@Service("mappingRecordService")
@AllArgsConstructor
public class MappingRecordServiceImpl extends ServiceImpl<MappingRecordMapper, MappingRecordEntity> implements MappingRecordService {

    private static final Logger log = LoggerFactory.getLogger(MappingRecordServiceImpl.class);

    private SnowflakeService snowflakeService;

    @Override
    public void saveMappingRecord(Long masterId, String name, String key, String value, Integer sort, Integer status, String remark) {

        // 获取正在保存键值对映射记录的用户对象
        UserPo userPo = UserUtils.getUser();
        Date date = new Date();

        MappingRecordEntity mappingRecord = new MappingRecordEntity();
        mappingRecord.setId(snowflakeService.getId())
                .setMasterId(masterId)
                .setMapName(name)
                .setMapKey(key)
                .setMapValue(value)
                .setSort(sort)
                .setStatus(status)
                .setUseStatus(MappingRecordEnum.USE_YES.getValue())
                .setDeleteStatus(CommonEnum.DELETE_NO.getValue())
                .setCreateTime(date)
                .setCreateUser(userPo.getUsername())
                .setCreateUserId(userPo.getId())
                .setRemark(remark);
        // 插入数据
        baseMapper.insert(mappingRecord);
        log.info("键值对映射记录数据插入完成");

    }

    @Override
    public void deleteUseMapping(Long masterId) {

        LambdaUpdateWrapper<MappingRecordEntity> mappingRecordLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置字段值
        mappingRecordLambdaUpdateWrapper.set(MappingRecordEntity::getUseStatus, MappingRecordEnum.USE_NO.getValue());
        // 匹配条件
        mappingRecordLambdaUpdateWrapper.eq(MappingRecordEntity::getMasterId, masterId)
                .eq(MappingRecordEntity::getUseStatus, MappingRecordEnum.USE_YES.getValue())
                .eq(MappingRecordEntity::getDeleteStatus, CommonEnum.DELETE_NO.getValue());
        // 更新数据
        int count = baseMapper.update(null, mappingRecordLambdaUpdateWrapper);
        log.info("取消正在使用的键值对映射数据完成。count={}", count);

    }
}